#include "StdAfx.h"
#include "ListHeaderItemUI.h"
#include "Rect.h"

CListHeaderItemUI::CListHeaderItemUI(void):m_uDragState(0)
{
}

CListHeaderItemUI::~CListHeaderItemUI(void)
{
}

LPCTSTR CListHeaderItemUI::GetClass() const
{
	return _T("ListHeaderItemUI");
}

void CListHeaderItemUI::SetText(LPCTSTR pstrText)
{
	m_sText = pstrText;
	UpdateLayout();
}

void CListHeaderItemUI::SetWidth(int cxWidth)
{
	m_cxWidth = cxWidth;
	UpdateLayout();
}

void CListHeaderItemUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("width")) == 0 ) SetWidth(_ttol(pstrValue));
	else CControlUI::SetAttribute(pstrName, pstrValue);
}

UINT CListHeaderItemUI::GetControlFlags() const
{
	return UIFLAG_SETCURSOR;
}

void CListHeaderItemUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
	{
		RECT rcSeparator = GetThumbRect(m_rcItem);
		if( ::PtInRect(&rcSeparator, event.ptMouse) ) {
			m_uDragState |= UISTATE_CAPTURED;
			ptLastMouse = event.ptMouse;
			m_pManager->SendNotify(this, _T("headerdragging"));
		}
		else {
			m_pManager->SendNotify(this, _T("headerclick"));
		}
	}
	if( event.Type == UIEVENT_BUTTONUP )
	{
		if( (m_uDragState & UISTATE_CAPTURED) != 0 ) {
			m_uDragState &= ~UISTATE_CAPTURED;
			m_pManager->SendNotify(this, _T("headerdragged"));
			m_pManager->UpdateLayout();
		}
	}
	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if( (m_uDragState & UISTATE_CAPTURED) != 0 ) {
			RECT rc = m_rcItem;
			rc.right -= ptLastMouse.x - event.ptMouse.x;
			const int MIN_DRAGSIZE = 40;
			if( rc.right - rc.left > MIN_DRAGSIZE ) {
				m_rcItem = rc;
				m_cxWidth = rc.right - rc.left;
				ptLastMouse = event.ptMouse;
				m_pParent->Invalidate();
			}
		}
	}
	if( event.Type == UIEVENT_SETCURSOR )
	{
		RECT rcSeparator = GetThumbRect(m_rcItem);
		if( IsEnabled() && ::PtInRect(&rcSeparator, event.ptMouse) ) {
			::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
			return;
		}
	}
	CControlUI::Event(event);
}

SIZE CListHeaderItemUI::EstimateSize(SIZE /*szAvailable*/)
{
	return CStdSize(m_cxWidth, 14 + m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight);
}

void CListHeaderItemUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	// Paint text (with some indent)
	RECT rcMessage = m_rcItem;
	rcMessage.left += 6;
	rcMessage.bottom -= 1;
	int nLinks = 0;
	CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcMessage, m_sText, UICOLOR_HEADER_TEXT, UICOLOR__INVALID, NULL, nLinks, DT_SINGLELINE | DT_VCENTER);
	// Draw gripper
	POINT ptTemp = { 0 };
	RECT rcThumb = GetThumbRect(m_rcItem);
	RECT rc1 = { rcThumb.left + 2, rcThumb.top + 4, rcThumb.left + 2, rcThumb.bottom - 1 };
	CBlueRenderEngineUI::DoPaintLine(hDC, m_pManager, rc1, UICOLOR_HEADER_SEPARATOR);
	RECT rc2 = { rcThumb.left + 3, rcThumb.top + 4, rcThumb.left + 3, rcThumb.bottom - 1 };
	CBlueRenderEngineUI::DoPaintLine(hDC, m_pManager, rc2, UICOLOR_STANDARD_WHITE);
}

RECT CListHeaderItemUI::GetThumbRect(RECT rc) const
{
	return CStdRect(rc.right - 4, rc.top, rc.right, rc.bottom - 3);
}